import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'test_rx_worker_page_controller.dart';



class User {
  String name;
  User({required this.name});
}
class test_rx_worker_page extends StatelessWidget {
  test_rx_worker_page({Key? key}) : super(key: key);

  final controller = Get.put(WorkerTestController());
  final user = User(name: 'Tom').obs;
  var name1 = ''.obs;
  var name2 = ''.obs;
  Worker? nameWorker;
  @override
  StatelessElement createElement() {
    nameWorker = ever(name1, (_){

      name2.value = name1.value;

    });
    return super.createElement();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("GetX Worker 测试")),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 显示 count
            Obx(() => Text(
              "Count: ${controller.count}",
              style: const TextStyle(fontSize: 24),
            )),
            Text(
              "name2: ${name2}",
              style: const TextStyle(fontSize: 24),
            ),
            const SizedBox(height: 20),

            // 按钮：点击 +1
            ElevatedButton(
              onPressed: controller.increment,
              child: const Text("点我 +1"),
            ),
            // 修改name1
            ElevatedButton(
              onPressed: (){
                name1.value += 'name1';
              },
              child: const Text("点我 +1"),
            ),
            const SizedBox(height: 20),

            // 搜索框：模拟防抖
            TextField(
              decoration: const InputDecoration(
                labelText: "搜索关键字",
                border: OutlineInputBorder(),
              ),
              onChanged: controller.onSearchChanged,
            ),

            const SizedBox(height: 20),

            // 返回按钮，测试 worker 释放
            ElevatedButton(
              onPressed: () {
                Get.back();
              },
              child: const Text("关闭页面，测试 Worker 释放"),
            ),

            // 测试refresh
            ElevatedButton(
              onPressed: () {
               user.value.name+='1';
               // 刷新本身，使本身非obs的属性也刷新
               user.refresh();
              },
              child:   ObxValue((data){
                return Text(data.value.name);
              }, user),
            ),
            
          ],
        ),
      ),
    );
  }
}
